﻿#include "findfileform.h"
#include "ui_findfileform.h"


FindFileForm::FindFileForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FindFileForm)
{
    ui->setupUi(this);
    ui-> m_StatusLabel->setText(tr("就绪"));
    ui-> m_ResultLabel->setText(tr("找到0个文件"));

    ui-> m_NameComboBox->setEditText(tr("*"));
    ui-> m_NameComboBox->addItem(tr("*"));
    ui-> m_DirComboBox->setEditText(QDir::currentPath());
    ui-> m_DirComboBox->addItem(QDir::currentPath());
    ui-> m_SensitiveCheckBox->setEnabled(false);
    ui-> m_StopPushButton->setEnabled(false);

    connect(ui-> m_StopPushButton,SIGNAL(clicked()),this,SLOT(OnStop()));
    connect(ui-> m_SearchPushButton,SIGNAL(clicked()),this,SLOT(OnSearch()));
    connect(ui-> m_BrowsePushButton,SIGNAL(clicked()),this,SLOT(OnBrowse()));
    connect(ui-> m_ClosePushButton,SIGNAL(clicked()),this,SLOT(OnClose()));
    connect(ui-> m_TxtlineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(OnTextChanged(const QString &)));

    setLayout(ui->m_MainVerticalLayout);
    resize(500,500);

    m_Stoped = false;

    // ui->setupUi(this);
}

FindFileForm::~FindFileForm()
{
    delete ui;
}


void FindFileForm::OnSearch()
{
    m_Stoped = false;
    m_nCOunt = 0;
    bool bCaseSensetive = ui -> m_SensitiveCheckBox->isChecked();
    bool bSubfolder = ui -> m_SubfilderCheckBox->isChecked();
    QString dir = ui -> m_DirComboBox->currentText();
    QString content = ui -> m_TxtlineEdit->text();
    QString name = ui -> m_NameComboBox->currentText();

    //m_UIForm -> frame->setEnabled(false);

    ui -> m_SearchPushButton->setEnabled(false);
    ui -> m_StopPushButton->setEnabled(true);
    ui -> m_ResultTableWidget->setRowCount(0);

    ExecuteSearch(QDir(dir),name,content);

    if(m_Stoped)
    {
        ui->m_StatusLabel->setText(tr("中止"));
    }
    else
    {
        ui->m_StatusLabel->setText(tr("就绪"));
    }

    //m_UIForm->frame->setEnabled(true);

    ui->m_SearchPushButton->setEnabled(true);
    ui->m_StopPushButton->setEnabled(false);

}
void FindFileForm::OnBrowse()
{
    QString dir =  QFileDialog::getExistingDirectory(this,tr("浏览文件目录"),QDir::currentPath());
    ui->m_DirComboBox->addItem(dir);
    ui->m_DirComboBox->setCurrentIndex(ui->m_DirComboBox->currentIndex()+1);

}
void FindFileForm::OnStop()
{
    m_Stoped = true;
}
void FindFileForm::OnClose()
{
    QWidget::close();
}
void FindFileForm::OnTextChanged(const QString & content)
{
    if(content.isEmpty())
    {
        ui->m_SensitiveCheckBox->setEnabled(false);
    }
    else
    {
        ui->m_SensitiveCheckBox->setEnabled(true);
    }
}

void FindFileForm::ExecuteSearch(const QDir& dir,const QString& name,const QString& content)
{

    if(ui->m_SubfilderCheckBox->isChecked())
    {
        QStringList folders = dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
        for (int i = 0 ; i < folders.size(); i++)
        {
            qApp->processEvents();
            if(m_Stoped)
            {
                break;
            }
            QString strDir = QString(tr("%1/%2")).arg(dir.absolutePath()).arg(folders[i]);
            ExecuteSearch(strDir,name,content);
        }
    }

    QStringList files = FindFiles(dir,name,content);
    ShowFiles(dir,files);
    m_nCOunt += files.size();
    ui->m_ResultLabel->setText(tr("找到%1个文件").arg(m_nCOunt));
}

QStringList FindFileForm::FindFiles(const QDir& dir,const QString& name,const QString& content)
{
    QStringList files = dir.entryList(QStringList(name),QDir::Files|QDir::NoSymLinks);
    if(content.isEmpty())
        return files;

    Qt::CaseSensitivity sensitive = ui->m_SensitiveCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;

    QStringList findFiles;
    for (int i = 0 ; i < files.size() ; i++)
    {
        qApp->processEvents();
        if (m_Stoped)
        {
            break;
        }
        QFile file(dir.absoluteFilePath(files[i]));
        if (file.open(QIODevice::ReadOnly))
        {
            QString line;
            QTextStream instream(&file);
            while(!instream.atEnd())
            {
                if (m_Stoped)
                {
                    break;
                }

                line = instream.readLine();
                if (line.contains(content,sensitive))
                {
                    findFiles<<files[i];
                    break;
                }
            }
        }
    }

    return findFiles;
}

void FindFileForm::ShowFiles(const QDir& dir,const QStringList& files)
{
    for(int i = 0 ; i < files.size();i++)
    {
        QFile file(dir.absoluteFilePath(files[i]));
        QFileInfo fileInfo(file);
        QString name = fileInfo.fileName();
        qint64 sizes = fileInfo.size();
        QString time =  fileInfo.lastModified().toString(tr("yyyy年yy月yy日"));
        QString permit = fileInfo.isWritable()?tr("可写"):tr("可读");
        QTableWidgetItem* pNameItem = new QTableWidgetItem(name);
        QTableWidgetItem* pPermitItem = new QTableWidgetItem(permit);
        QTableWidgetItem* pTimeItem = new QTableWidgetItem(time);
        QTableWidgetItem* pSizeItem = new QTableWidgetItem(tr("%1KB").arg(int(sizes+1023)/1024));

        int rowcount = ui -> m_ResultTableWidget->rowCount();
        ui->m_ResultTableWidget->insertRow(rowcount);
        ui->m_ResultTableWidget->setItem(rowcount,0,pNameItem);
        ui->m_ResultTableWidget->setItem(rowcount,1,pSizeItem);
        ui->m_ResultTableWidget->setItem(rowcount,2,pTimeItem);
        ui->m_ResultTableWidget->setItem(rowcount,3,pPermitItem);
    }
}
